package cn.donting.jme.edit.ui.inspector.imp.transform;

import cn.donting.fx.ui.NumberField;
import cn.donting.jme.edit.EditorState;
import com.jme3.math.Transform;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import lombok.Data;

@Data
public class TransformController  {

    private Spatial spatial;

    @FXML
    public NumberField translationX;
    @FXML
    public NumberField translationY;
    @FXML
    public NumberField translationZ;
    @FXML
    public NumberField rotationX;
    @FXML
    public NumberField rotationY;
    @FXML
    public NumberField rotationZ;
    @FXML
    public NumberField scaleX;
    @FXML
    public NumberField scaleY;
    @FXML
    public NumberField scaleZ;



    public void setSpatial(Spatial spatial) {
        this.spatial = spatial;
        update();
    }

    private void update() {
        Transform localTransform = spatial.getLocalTransform();
        Vector3f translation = localTransform.getTranslation();
        translationX.setValue(translation.x);
        translationY.setValue(translation.y);
        translationZ.setValue(translation.z);
        float[] angles = localTransform.getRotation().toAngles(null);
        rotationX.setValue(angles[0]);
        rotationY.setValue(angles[1]);
        rotationZ.setValue(angles[2]);
        Vector3f scale = localTransform.getScale();
        scaleX.setValue(scale.x);
        scaleY.setValue(scale.y);
        scaleZ.setValue(scale.z);
        //:TODO: 改变spatial的localTransform的值

        translationX.floatProperty.addListener((observable, oldValue, newValue) -> EditorState.getJmeEditSceneApp().enqueue(() ->
                spatial.setLocalTranslation(newValue.floatValue(),translationY.getFloat(),translationZ.getFloat())));



    }
}
